<?php
//session_start();
$title="แสดงรายการจองทั้งหมด";
//include ('header-admin.php');

/*$type;
if(isset($_GET['type'])){
	$type = $_GET['type'];
}else{
	$type="0";
}

$page = (isset($_GET['page']))? intval($_GET['page']) : 1;
$limit_end = 10;
$limit_start = ($page-1)*$limit_end;

function page_navi($page, $limit, $range, $count, $send=NULL, $current_style="navi_on", $other_style="navi_out", $target="_self"){
    $output = "";
    $total = ceil($count/$limit);
    $navi_start = $page-$range;
    $navi_end = $page+$range;
     
    $send .= (!empty($send))? "&" : NULL;
         
    if($navi_start <= 0) $navi_start = 1;
    if($navi_end >= $total) $navi_end = $total;
    
	$type;
	if(isset($_GET['type'])){
		$type = $_GET['type'];
	}else{
		$type="1";
	}
     
    if($page>1){
        $navi_back = $page-1;
        if($page > 2)
        $output .= "<a href=\"?" . $send . "type=".$type."&page=1\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>&laquo;</strong></a> ";
        $output .= "<a href=\"?" . $send . "type=".$type."&page=" . $navi_back . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>&#8249;</strong></a> ";
    }
    for($i = $navi_start; $i <= $navi_end; $i++){
        if($i == $page)
        $output .= "<a href=\"?" . $send . "type=".$type."&page=" . $i . "\" target=\"" . $target . "\" class=\"" . $current_style . "\"><strong><font color=\"" . $other_style . "\">$i</font></strong></a> ";
        else
        $output .= "<a href=\"?" . $send . "type=".$type."&page=" . $i . "\" target=\"" . $target . "\" class=\"" . $other_style . "\">$i</a> ";
    }
    if($page < $total){
        $navi_next = $page+1;
        $output .= "<a href=\"?" . $send . "type=".$type."&page=" . $navi_next . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>&#8250;</strong></a> ";
        if(($page+1) < $total)
        $output .= "<a href=\"?" . $send . "type=".$type."&page=" . $total . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>&raquo;</strong></a>";
    }
    if($navi_start>$navi_end)
        $output .= "<a href=\"?" . $send . "type=".$type."&page=" . $page . "\" target=\"" . $target . "\" class=\"" . $other_style . "\"><strong>$page</strong></a> ";
    return $output;
}*/


//$menu = array('แสดงรายการจองทั้งหมด','แสดงรายการจองที่ยังไม่ Confirm');

$countTopic=0;
?>
<script language="javascript">
	function ConfirmChoice_confirm(id){
	answer = confirm("คุณแน่ใจว่าจะยืนยันการจอง ?")
		if (answer !=0)
			location = "doConfirmBooking.php?reserv_id=" + id;
	}
	function ConfirmChoice_del(id){
	answer = confirm("คุณแน่ใจว่าจะลบ ?")
		if (answer !=0)
			location = "doDeleteBooking.php?reserv_id=" + id;
	}
	function viewBooking(){
/* 	$('#viewBooking').submit(); */
		/*
var bodyContent = $.ajax({
      url: "viewBooking.php",
      global: false,
      type: "POST",
      dataType: "html",
      async:false}).responseText;
*/
	}
	function printConfirmPage(id){
		window.open('print.php?ref=p_confirm&reserv_gid='+id, 'printpage', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=800,height=480')
                      return false;
	}
</script>
<div id="center-block" class="main-content">
<img src="img/calendar.png" id="img-block-content">
<div id="content-title-right" style="margin-top: -24px;">
		<h2><?//=$menu[$type-1]?></h2>
		<span class="caption">รายการสถานะการจองทั้งหมด</span>
		<!--<span class="right-button">
		   <form name="jump"><select name="menu" id="menu" style="width: 150px" onChange="location=document.jump.menu.options[document.jump.menu.selectedIndex].value;">
		      <?
		      /*$select_str = 'selected="selected"';
		      $a = 1;
		      foreach($menu as $text){
		         if($type == $a)
		            echo '<option value="listBooking.php?type=' . $a . '" selected="selected">' . $text . '</option>';
		         else
		            echo '<option value="listBooking.php?type=' . $a . '">' . $text . '</option>';
		         $a++;
		      }*/
		      ?>
			</select></form>
				</span>
	--></div>

	<hr>
	<!--<table summary="webboard about Datastrucutre learning" id="table">
		<thead>
		<tr>
		<th scope="col" abbr="reserv_id">เลขที่</th>
		<th scope="col" abbr="reserv_begin">วันที่พัก</th>
		<th scope="col" abbr="room_id">ห้อง</th>
		<th scope="col" abbr="customer_name">ชื่อ-นามสกุล ผู้จอง</th>
		<th scope="col" abbr="customer_name">สถานะ</th>
		<th scope="col" abbr="customer_name">รายละเอียด</th>
		<th scope="col" abbr="customer_name">จัดการ</th>
		</tr>	
		</thead>
		<tbody>-->
		
		<?
		$current = date("Y-m-d",strtotime("Today"));
	/*if($type=="0")
	   $sql="SELECT reservgroup.reserv_gid AS reserv_gid, reservgroup.room_list AS room_list, reservation.reserv_date AS reserv_date, reservation.reserv_begin AS reserv_begin, reservation.reserv_end AS reserv_end, reservation.reserv_etc AS reserv_etc, customer.customer_name AS customer_name, customer.customer_surname AS customer_surname, 
	   		MAX(reservation.reserv_status) AS reserv_current_status
			FROM reservgroup
			INNER JOIN customer ON ( reservgroup.customer_id = customer.id )
			INNER JOIN reservation ON ( reservgroup.reserv_gid = reservation.reserv_gid )
   			WHERE reserv_status < '3'
   			GROUP BY reserv_gid
   			ORDER BY reserv_begin ASC";
	   
	else if($type=="1")
	   $sql="SELECT reservgroup.reserv_gid AS reserv_gid, reservgroup.room_list AS room_list, reservation.reserv_date AS reserv_date, reservation.reserv_begin AS reserv_begin, reservation.reserv_end AS reserv_end, reservation.reserv_etc AS reserv_etc, customer.customer_name AS customer_name, customer.customer_surname AS customer_surname
			FROM reservgroup
			INNER JOIN customer ON ( reservgroup.customer_id = customer.id )
			INNER JOIN reservation ON ( reservgroup.reserv_gid = reservation.reserv_gid )
			WHERE reserv_status != '2'
			GROUP BY reserv_gid
			ORDER BY reserv_begin ASC";

   else if($type=="2")
      $sql="SELECT reservgroup.reserv_gid AS reserv_gid, reservgroup.room_list AS room_list, reservation.reserv_date AS reserv_date, reservation.reserv_begin AS reserv_begin, reservation.reserv_end AS reserv_end, reservation.reserv_etc AS reserv_etc, customer.customer_name AS customer_name, customer.customer_surname AS customer_surname
			FROM reservgroup
			INNER JOIN customer ON ( reservgroup.customer_id = customer.id )
			INNER JOIN reservation ON ( reservgroup.reserv_gid = reservation.reserv_gid )
			WHERE reservation.reserv_status = '0'
			GROUP BY reserv_gid
			ORDER BY reserv_id DESC";*/

   /*else if($type=="3")
      $sql="SELECT *
   FROM customer
   INNER JOIN reservation ON (customer.id = reservation.id)
   INNER JOIN  resort ON (resort.room_id = reservation.room_id)
   WHERE resort.room_status != '0' AND reservation.reserv_status = '1' AND '".$current."' BETWEEN reservation.reserv_begin AND reservation.reserv_end
   ORDER BY reserv_id DESC";*/
   
      
      $sql="SELECT reservgroup.reserv_gid AS reserv_gid, reservgroup.room_list AS room_list, reservation.reserv_date AS reserv_date, reservation.reserv_begin AS reserv_begin, reservation.reserv_end AS reserv_end, reservation.reserv_etc AS reserv_etc, customer.customer_name AS customer_name, customer.customer_surname AS customer_surname, 
	   		MAX(reservation.reserv_status) AS reserv_current_status, reservgroup.reserv_gdetail AS reserv_gdetail,
	   		reservgroup.reserv_gcost AS reserv_gcost,
	   		reservgroup.reserv_cashpledge AS reserv_cashpledge
			FROM reservgroup
			INNER JOIN customer ON ( reservgroup.customer_id = customer.id )
			INNER JOIN reservation ON ( reservgroup.reserv_gid = reservation.reserv_gid )
   			WHERE reserv_status < '4'
   			GROUP BY reserv_gid
   			ORDER BY reserv_begin ASC, reserv_gid ASC";

	//$result = mysql_query($sql. " LIMIT " . $limit_start. "," . $limit_end);
	$result = mysql_query($sql);
	$count = mysql_num_rows($result);
	$total = mysql_num_rows(mysql_query($sql));
	$status = " ";
	$color = 0;
	$table_notconfirm = "";
	$table_confirmed = "";
	$table_checkined = "";
if($total > 0){
	while ($rs = mysql_fetch_array($result)){
		if($rs['reserv_current_status']<=1){
			$status = 'ยังไม่เรียบร้อย <img src="img/bullet_red.png" style="vertical-align: middle;">';
			if($table_notconfirm_color%2 == 0){
				$table_notconfirm .= '<tr class="odd" ondblclick="viewBooking()">';
			}else{
				$table_notconfirm .= '<tr class="even" ondblclick="viewBooking()">';
			}
			//echo '<tr class="odd">';
			$table_notconfirm .= '<td>' . $rs['reserv_gid'] . '</a>';
			$table_notconfirm .= '</td>';
			$table_notconfirm .= '<td>' . $rs['reserv_begin'] . ' - ' .  $rs['reserv_end'] . '</td>';
			$table_notconfirm .= '<td width="150px">' . $rs['room_list'] . '</td>';
			$table_notconfirm .= '<td>' . $rs['customer_name'].' '.$rs['customer_surname']. '</td>';
			$table_notconfirm .= '<td>' . $status . '</td>';
/* 			$table_notconfirm .= '<td>' . $rs['reserv_gdetail'] . '</td>'; */
			$table_notconfirm .= '<td>' . number_format($rs['reserv_gcost']) . '</td>';
			$table_notconfirm .= '<td>' . number_format($rs['reserv_cashpledge']) . '</td><td>';
			/* if($rs['reserv_current_status']==0) $table_notconfirm .= '<a href="#" onclick="ConfirmChoice_confirm(\'' . $rs['reserv_gid'] . '\'); return false;"><img src="img/icon_confirm.gif" /></a>&nbsp;'; */
			$table_notconfirm .= '<a href="#" onclick="printConfirmPage(\'' . $rs['reserv_gid'] . '\'); return false;"><img src="img/m_icon_printer.png" /></a>&nbsp;<a href="editBooking.php?reserv_id=' . $rs['reserv_gid'] . '&reserv_begin_o='. $rs['reserv_begin'].'&reserv_end_o='. $rs['reserv_end'].'"><img src="img/icon_edit.png" /></a>&nbsp;<a href="#" onclick="ConfirmChoice_del(\'' . $rs['reserv_gid'] . '\'); return false;"><img src="img/icon_delete.png" /></a>';
			$table_notconfirm .= '</td></tr>';
			$table_notconfirm_color++;
		}
		if($rs['reserv_current_status']==2){
			$status = 'ยังใม่เรียบร้อย <img src="img/bullet_yellow.png" style="vertical-align: middle;">';
/* 			$status = 'เรียบร้อยแล้ว <img src="img/bullet_yellow.png" style="vertical-align: middle;">'; */
			/*
if($rs['reserv_current_status']==1){
				$status = 'ยังไม่เรียบร้อย <img src="img/bullet_red.png" style="vertical-align: middle;">';
			}
*/			
			if($table_confirmed_color%2 == 0){
				$table_confirmed .= '<tr class="odd" ondblclick="viewBooking()">';
			}else{
				$table_confirmed .= '<tr class="even" ondblclick="viewBooking()">';
			}
			//echo '<tr class="odd">';
			$table_confirmed .= '<td>' . $rs['reserv_gid'] . '</a>';
			$table_confirmed .= '</td>';
			$table_confirmed .= '<td>' . $rs['reserv_begin'] . ' - ' .  $rs['reserv_end'] . '</td>';
			$table_confirmed .= '<td width="150px">' . $rs['room_list'] . '</td>';
			$table_confirmed .= '<td>' . $rs['customer_name'].' '.$rs['customer_surname']. '</td>';
			$table_confirmed .= '<td>' . $status . '</td>';
/* 			$table_confirmed .= '<td>' . $rs['reserv_gdetail'] . '</td>'; */
			$table_confirmed .= '<td>' . number_format($rs['reserv_gcost']) . '</td>';
			$table_confirmed .= '<td>' . number_format($rs['reserv_cashpledge']) . '</td><td>';
			/* if($rs['reserv_current_status']==0) $table_confirmed .= '<a href="#" onclick="ConfirmChoice_confirm(\'' . $rs['reserv_gid'] . '\'); return false;"><img src="img/icon_confirm.gif" /></a>&nbsp;'; */
			$table_confirmed .= '<a href="#" onclick="printConfirmPage(\'' . $rs['reserv_gid'] . '\'); return false;"><img src="img/m_icon_printer.png" /></a>&nbsp;<a href="editBooking.php?reserv_id=' . $rs['reserv_gid'] . '&reserv_begin_o='. $rs['reserv_begin'].'&reserv_end_o='. $rs['reserv_end'].'"><img src="img/icon_edit.png" /></a>&nbsp;<a href="#" onclick="ConfirmChoice_del(\'' . $rs['reserv_gid'] . '\'); return false;"><img src="img/icon_delete.png" /></a>';
			$table_confirmed .= '</td></tr>';
			$table_confirmed_color++;
		}
		if($rs['reserv_current_status']==3){
			$status = 'เรียบร้อยแล้ว <img src="img/bullet_green.png" style="vertical-align: middle;">';

			if($table_checkined_color%2 == 0){
				$table_checkined .= '<tr class="odd">';
			}else{
				$table_checkined .= '<tr>';
			}
			//echo '<tr class="odd">';
			$table_checkined .= '<td>' . $rs['reserv_gid'] . '</a>';
			$table_checkined .= '</td>';
			$table_checkined .= '<td>' . $rs['reserv_begin'] . ' - ' .  $rs['reserv_end'] . '</td>';
			$table_checkined .= '<td width="150px">' . $rs['room_list'] . '</td>';
			$table_checkined .= '<td>' . $rs['customer_name'].' '.$rs['customer_surname']. '</td>';
			$table_checkined .= '<td>' . $status . '</td>';
/* 			$table_checkined .= '<td>' . $rs['reserv_etc'] . '</td><td>'; */
			$table_checkined .= '<td>' . number_format($rs['reserv_gcost']) . '</td>';
			$table_checkined .= '<td>' . number_format($rs['reserv_cashpledge']) . '</td><td>';
			if($rs['reserv_current_status']==0) $table_checkined .= '<a href="#" onclick="ConfirmChoice_confirm(\'' . $rs['reserv_gid'] . '\'); return false;"><img src="img/icon_confirm.gif" /></a>&nbsp;';
			$table_checkined .= '<a href="#" onclick="printConfirmPage(\'' . $rs['reserv_gid'] . '\'); return false;"><img src="img/m_icon_printer.png" /></a>&nbsp;<a href="editBooking.php?reserv_id=' . $rs['reserv_gid'] . '&reserv_begin_o='. $rs['reserv_begin'].'&reserv_end_o='. $rs['reserv_end'].'"><img src="img/icon_edit.png" /></a>&nbsp;<a href="#" onclick="ConfirmChoice_del(\'' . $rs['reserv_gid'] . '\'); return false;"><img src="img/icon_delete.png" /></a>';
			$table_checkined .= '</td></tr>';
			$table_checkined_color++;
		}
		//if($rs['reserv_current_status']==3)$status = 'เช็คเอาท์แล้ว<img src="img/m_icon_printer.png" style="vertical-align: middle;">';
	}

//echo "\n<div style=\"float:left;\">Page : " . page_navi($page, $limit_end, 4, $total, null) . " </div><div style=\"float:right;\">[" . ($limit_start+1) . "-" . ($limit_start+$count) . "] of <strong>" . $total . "</strong></div>";
}
	?>
	<table summary="Table not confirm" id="table">
		<thead>
		<tr>
		<th scope="col" abbr="reserv_id">เลขที่จอง</th>
		<th scope="col" abbr="reserv_begin">วันที่เข้าพัก</th>
		<th scope="col" abbr="room_id">ห้อง</th>
		<th scope="col" abbr="customer_name">ชื่อ-นามสกุล ผู้จอง</th>
		<th scope="col" abbr="customer_name">สถานะ</th>
<!-- 		<th scope="col" abbr="customer_name">รายละเอียด</th> -->
		<th scope="col" abbr="customer_name">จำนวนเงิน</th>
		<th scope="col" abbr="customer_name">มัดจำ</th>
		<th scope="col" abbr="customer_name">จัดการ</th>
		</tr>	
		</thead>
		<tbody>
		<?php echo $table_notconfirm;?>
		</tbody>
	</table>
	
	<table summary="Table confirmed" id="table">
		<thead>
		<tr>
		<th scope="col" abbr="reserv_id">เลขที่จอง</th>
		<th scope="col" abbr="reserv_begin">วันที่เข้าพัก</th>
		<th scope="col" abbr="room_id">ห้อง</th>
		<th scope="col" abbr="customer_name">ชื่อ-นามสกุล ผู้จอง</th>
		<th scope="col" abbr="customer_name">สถานะ</th>
<!-- 		<th scope="col" abbr="customer_name">รายละเอียด</th> -->
		<th scope="col" abbr="customer_name">จำนวนเงิน</th>
		<th scope="col" abbr="customer_name">มัดจำ</th>
		<th scope="col" abbr="customer_name">จัดการ</th>
		</tr>	
		</thead>
		<tbody>
		<?php echo $table_confirmed;?>
		</tbody>
	</table>
	
	<table summary="table checkined" id="table">
		<thead>
		<tr>
		<th scope="col" abbr="reserv_id">เลขที่จอง</th>
		<th scope="col" abbr="reserv_begin">วันที่เข้าพัก</th>
		<th scope="col" abbr="room_id">ห้อง</th>
		<th scope="col" abbr="customer_name">ชื่อ-นามสกุล ผู้จอง</th>
		<th scope="col" abbr="customer_name">สถานะ</th>
<!-- 		<th scope="col" abbr="customer_name">รายละเอียด</th> -->
		<th scope="col" abbr="customer_name">จำนวนเงิน</th>
		<th scope="col" abbr="customer_name">มัดจำ</th>
		<th scope="col" abbr="customer_name">จัดการ</th>
		</tr>	
		</thead>
		<tbody>
		<?php echo $table_checkined;?>
		</tbody>
	</table>
	
	</div>
	<form action="viewBooking.php" method="post" id="viewBooking">
	</form>

<?
//include ('footer.php');
?>